package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author：郑谦
 * @时间：2017-3-20 下午3:47:52
 * @类名：AdviseActivity.java
 * @功能: 个人中心——意见反馈
 */
public class AdviseActivity extends Activity {

    HttpTools toolModel;
    /**
     * 返回按钮
     */
    private ImageButton ib_fanhui;
    /**
     * 意见content
     */
    private EditText et_advise;
    /**
     * 提交
     */
    private Button btn_tijiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_advise);
        initRes();
    }

    private void initRes() {
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        ib_fanhui.setVisibility(View.VISIBLE);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_advise = (EditText) findViewById(R.id.et_advise);
        et_advise.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                et_advise.setCursorVisible(true);
            }
        });
        btn_tijiao = (Button) findViewById(R.id.btn_submit);
        btn_tijiao.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                toolModel = new HttpTools(AdviseActivity.this);
                String content = et_advise.getText().toString();
                SharedPreferences sp = getSharedPreferences("spReg",
                        Context.MODE_PRIVATE);
                String token = sp.getString("data", "");
                if ("".equals(content) || content == null) {
                    Toast.makeText(getApplicationContext(), "请输入您的反馈意见!", Toast.LENGTH_SHORT).show();
                } else {
                    toolModel.saveAdvise(token, content, new JsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(int statusCode, Header[] headers,
                                              JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            try {
                                Log.e("zq", "意见反馈----" + response.toString());
                                boolean code = response.getBoolean("code");
                                String msg = response.getString("msg");
                                if (code == true) {
//								MyToast.toast(getApplicationContext(), msg);
                                    Toast.makeText(AdviseActivity.this, msg, Toast.LENGTH_SHORT).show();
                                    et_advise.setText("");
                                } else {
//								MyToast.toast(getApplicationContext(), msg);
                                    Toast.makeText(AdviseActivity.this, msg, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    });
                }

            }
        });
    }


}
